package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.BootstrapText;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.MissionItemAdapter;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.MissionE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.MissionBiz;
import com.infore.airsupervise.biz.impl.MissionBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * @features: 公共的任务列表搜索界面
 * @author: WuTao
 * @date: 2017/5/5 16:54
 */
public class NewMissionQueryActivity extends BaseTitleBarActivity implements SwipeRecyclerView.OnActionListener {
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView recyclerView;

    private MissionBiz missionBiz;

    private int missionType ;
    private MissionItemAdapter adapter;
    private List<MissionE> list = new ArrayList<>();//列表集合

    private int from ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_mission_query);
        ButterKnife.bind(this);
        try{
            Intent it = getIntent();
            missionType = it.getIntExtra(Constants.INTENT_INT_KEY,0);
            from = it.getIntExtra("from" , 0);
            initViews();
            missionBiz = new MissionBizImpl();
        }catch (Exception ex){
            ToastUtil.showShort(ctx, "数据传递错误，请回退到上一级");
        }
    }

    private void initViews() {
        title.setText("任务检索");
        rightMenu.setVisibility(View.INVISIBLE);
        BootstrapText.Builder  builder= new BootstrapText.Builder(ctx);
        builder.addText(getTypeDetail()).addFontAwesomeIcon(FontAwesome.FA_CHEVRON_LEFT);
        leftMenu.setBootstrapText(builder.build());
        tvCancel.setText("搜索");
        initRecylceView();
    }
    private void initRecylceView(){
        adapter = new MissionItemAdapter(R.layout.item_current_mission,list);
        recyclerView.setAdapter(adapter);
        recyclerView.setActionListener(this);
        recyclerView.getSwipeLayout().setEnabled(false);
    }

    private void getMissionData(final boolean isRefresh){
        Call call =missionBiz.sendMissionListRequest(
                UserE.getUserInfo().getUserId(), missionType,
                etSearch.getText().toString(), null, null, 0,
                new OnServerResponseListener<List<MissionE>>() {

            @Override
            public void onSucceed(List<MissionE> result, String desc) {
                recyclerView.commonSuccessHandle(isRefresh, result.size());
                if (isRefresh) {
                    list.clear();
                }
                list.addAll(result);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(int errorCode, String title, String desc) {
                recyclerView.commonFailedHandle(isRefresh,errorCode,title,desc);
            }
        });
        requests.add(call);
    }

    @OnClick({R.id.iv_searchClean, R.id.tv_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_searchClean:
                etSearch.getText().clear();
                break;
            case R.id.tv_cancel://搜索
                recyclerView.getSwipeLayout().setRefreshing(true);
                recyclerView.onRefresh();
                break;
        }
    }

    @Override
    public void onRefresh() {
        getMissionData(true);
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.getAdapter().loadMoreEnd();
                recyclerView.getSwipeLayout().setEnabled(true);
            }
        }, 1000);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(from == 1){
            Intent it = new Intent();
            it.putExtra(Constants.INTENT_STR_KEY,list.get(position).getId());
            setResult(RESULT_OK,it);
            finish();
        }else {
            JumpUtil.jumpFromTo(this, NewMissionDetailActivity.class, Constants.INTENT_OBJ_KEY, list.get(position));
        }
    }

    private String getTypeDetail(){
        String str = "";
        switch (missionType){
            case StatusConst.MISSION_DB :
                str ="待办";
                break;
            case StatusConst.MISSION_YB :
                str=  "已办";
                break;
            case StatusConst.MISSION_DS :
                str= "待审";
                break;
            case StatusConst.MISSION_YS :
                str= "已审";
                break;
            case StatusConst.MISSION_ZP :
                str= "指派";
                break;
        }
        return str;
    }
}
